let host = "cn-beijing.log.aliyuncs.com";
let project = "zhufeng"; //阿里服务项目名称
let logstore = "zhufeng_logstore";
// let userAgent  = require('user-agent')
function getEXtraData() {
  //自定义数据  比如用户的信息 浏览器类型
  return {
    title: document.title,
    url: location.url,
    timestamp: Date.now(),
    // userAgent:userAgent(navigator.userAgent)
  };
}

//把错误日志上报服务
class SendTracker {
  constructor() {
    this.xhr = new XMLHttpRequest;
    this.url = `http://${project}.${host}/logstores/${logstore}/track`; //地址
  }

  send(data = {}) {
    let EXtraData = getEXtraData();
    let log = { ...EXtraData, ...data };
    //阿里云要求 对象的值不能是数字
    for (let key in log) {
      if (typeof log[key] === "number") {
        log[key] = `${log[key]}`;
      }
    }
 
    let body = JSON.stringify({
      __log__:[log]
    }); //log 转成字符串
    // console.log(log, body,"log");
    // this.xhr.open("POST", this.url, true);
    // this.xhr.setRequestHeader("x-log-compresstype", "application/json"); //请求类型
    // this.xhr.setRequestHeader("x-log-apiversion", "0.6.0"); //接口API 版本号
    // this.xhr.setRequestHeader("x-log-bodyrawsize", body.length); //log 大小
    
    // this.xhr.onload = function () {
    //   console.log(this.xhr.response, "response");
    // };
    // this.xhr.onerror = function (error) {
    //   console.log(error, "error");
    // };
    // this.xhr.send(body);
    
  }
}

export default new SendTracker();
